package analyse;

import org.my.stock.util.MyUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 获取板块中股票的资金是持续流入的
 * */
public class Inflow_stock {

    public static List<List<Object>> inflow(String type_name) {
        List<List<Object>> lst = new ArrayList<>();

        Hashtable<String, List<Double>> ht3 = getStockFund(type_name, "2025-04-25", "2025-04-29");
        Hashtable<String, List<Double>> ht5 = getStockFund(type_name, "2025-04-23", "2025-04-29");
        for (String k : ht3.keySet()) {
            List<Object> vsr = new ArrayList<>();
            vsr.add(k);
            List<Double> v = ht3.get(k);
            List<Double> v1 = ht5.get(k);
            for (int i = 0; i < v.size(); i++) {
                vsr.add(v.get(i) - v1.get(i));
            }
            lst.add(vsr);
        }

        return lst.stream().sorted((a, b) -> {
            if ((double)a.get(2) - (double)b.get(2) > 0)
                return -1;
            return 1;
        }).collect(Collectors.toList());
    }

    public static Hashtable<String, List<Double>> getStockFund(String type_name, String begin, String end) {
        Hashtable<String, List<Double>> ht = new Hashtable<String, List<Double>>();
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            conn = MyUtil.getConnection();
            stmt = conn.prepareStatement("SELECT SYMBOL, name FROM TYPE_STOCK WHERE TYPE_NAME = '"+ type_name +"'");
            rs = stmt.executeQuery();
            while (rs.next()) {
                String symbol = rs.getString(1);
                List<Double> vs = getStockSymbolFund(symbol, begin, end);
                ht.put(rs.getString(2), vs);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 7. 关闭资源（反向顺序）
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return ht;
    }

    public static List<Double> getStockSymbolFund(String symbol, String begin, String end) {
        List<Double> lst = new ArrayList<>();
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            conn = MyUtil.getConnection();
            stmt = conn.prepareStatement("SELECT AVG(m.zhangdie), AVG(m.main_fund), AVG(m.main_fund_bf) FROM df_stock_date m WHERE m.SYMBOL = ? AND m.STOCK_DATE BETWEEN ? AND ?");
            stmt.setString(1, symbol);
            stmt.setDate(2, MyUtil.toDate(begin));
            stmt.setDate(3, MyUtil.toDate(end));
            rs = stmt.executeQuery();
            while (rs.next()) {
                lst.add(rs.getDouble(1));
                lst.add(rs.getDouble(2));
                lst.add(rs.getDouble(3));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 7. 关闭资源（反向顺序）
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return lst;
    }
}

















































